/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-17 17:00:55
 * Last Modified: 2017-04-19 11:46:38
 * File Name: controllers/cr.go
 * Description:
 */

package controllers

import (
	"encoding/json"
	"strings"

	"openapi/chinaregion"

	log "github.com/cihub/seelog"
)

type ChinaRegionController struct {
	BaseController
	//controllers.URLController
}

//
//	查询中国的省份列表
//
// @router /province [get]
func (p *ChinaRegionController) QueryProvince() {
	defer p.Output("QueryProvince")
	data := chinaregion.GetChinaRegionService().QueryChinaProvinces()
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//
//	查询中国的城市列表
//
// @router /city [get]
func (p *ChinaRegionController) QueryCity() {
	defer p.Output("QueryCity")
	regionId := p.GetString("regionid", "")
	data := chinaregion.GetChinaRegionService().QueryChinaCities(regionId)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//
//	查询中国的区县列表
//
// @router /county [get]
func (p *ChinaRegionController) QueryCounty() {
	defer p.Output("QueryCounty")
	regionId := p.GetString("regionid", "")
	data := chinaregion.GetChinaRegionService().QueryChinaCounties(regionId)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//
//	查询中国城镇列表
//
// @router /town [get]
func (p *ChinaRegionController) QueryTown() {
	defer p.Output("QueryTown")
	regionId := p.GetString("regionid", "")
	data := chinaregion.GetChinaRegionService().QueryChinaTowns(regionId)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//
//	根据编码反查行政信息
//
// @router /parse-region [get]
func (p *ChinaRegionController) ParseRegionCode() {
	defer p.Output("ParseRegionCode")
	regionCode := p.GetString("regionid", "")
	data := chinaregion.GetChinaRegionService().ParseRegionCode(regionCode)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//批量反查行政信息
// @router /parse-region-batch [get]
func (p *ChinaRegionController) ParseRegionCodeBatch() {
	defer p.Output("ParseRegionCodeBatch")
	regionCodes := strings.Split(p.GetString("regionid", ""), ",")
	results := make(map[string]*chinaregion.RegionInfo)
	for _, regionCode := range regionCodes {
		if _, ok := results[regionCode]; !ok {
			data := chinaregion.GetChinaRegionService().ParseRegionCode(regionCode)
			results[regionCode] = data
		}
	}
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = results
}

/*
	根据地址查询region_code
*/
// @router /addrtoregion [post]
func (p *ChinaRegionController) Addr2RegionCode() {
	defer p.Output("Addr2RegionCode")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	pi := make(map[string]string)
	if err := json.Unmarshal(body, &pi); err != nil {
		log.Warnf("InsertChannelPage error: %s", err.Error())
		p.Resp["code"] = 1
		p.Resp["msg"] = "参数不是合法的json格式"
		return
	}

	if strings.Contains(pi["city"], "区直辖") {
		pi["city"] = "区直辖"
	}

	if len(pi["province"]) < 6 || len(pi["city"]) < 6 || len(pi["county"]) < 6 {
		p.Resp["code"] = 1
		p.Resp["msg"] = "没有找到相应的省市区信息"
		p.Resp["data"] = ""
		return
	}

	if pi["province"][:6] == pi["city"][:6] {
		pi["city"] = "市辖区"
	}

	data := chinaregion.GetChinaRegionService().Adrr2RegionCode(pi["province"][:6], pi["city"][:6], pi["county"][:6])
	log.Info(pi["province"][:6], "--", pi["city"][:6], "--", pi["county"][:6])

	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	if data == "" {
		p.Resp["code"] = 1
		p.Resp["msg"] = "没有找到相应的省市区信息"
	}
	p.Resp["data"] = data
}
